package com.megvii.koala.tv.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.megvii.corelib.base.MegviiAdapter;
import com.megvii.koala.tv.R;
import com.megvii.koala.tv.data.CameraBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by linyue on 16/3/16.
 */
public class RecognizeCamerasSettingAdapter
        extends MegviiAdapter<RecognizeCamerasSettingAdapter.ViewHolder>
{
    private List<CameraBean> cameras;

    private List<Boolean> selections;

    public RecognizeCamerasSettingAdapter(Context context)
    {
        super(context, ViewHolder.class, R.layout.camera_item);
    }

    public void updateData(List<CameraBean> cameras)
    {
        this.cameras = cameras;
        this.selections = new ArrayList<>();
        if (cameras != null)
        {
            int size = cameras.size();
            for (int i = 0; i < size; i++)
            {
                this.selections.add(false);
            }
        }
        notifyDataSetChanged();
    }

    public void updateData(List<CameraBean> cameras, List<Boolean> selections)
    {
        this.cameras = cameras;
        this.selections = selections;
        notifyDataSetChanged();
    }

    public void toggleSelection(int index)
    {
        boolean selected = selections.get(index);
        selections.set(index, !selected);
        notifyDataSetChanged();
    }

    public List<CameraBean> getSelections()
    {
        List<CameraBean> result = new ArrayList<>();
        if (cameras != null)
        {
            int size = selections.size();
            for (int i = 0; i < size; i++)
            {
                if (selections.get(i))
                {
                    result.add(cameras.get(i));
                }
            }
        }
        return result;
    }

    /**
     * 根据摄像头的id获取摄像头在adapter中的position
     * @param id
     * @return
     */
    public int getCamPositionById(String id){
        for (int i = 0; i < cameras.size(); i++) {
            if(cameras.get(i).id.equals(id)){
                return i;
            }
        }
        return -1;
    }

    @Override
    protected void processViewHolder(ViewHolder viewHolder, int position)
    {
        CameraBean camera = cameras.get(position);
        boolean selected = selections.get(position);
        viewHolder.addressText.setText(camera.camera_position + "(" + camera.camera_address + ")");
        viewHolder.selectedView.setVisibility(selected ? View.VISIBLE : View.INVISIBLE);
    }

    @Override
    public int getCount()
    {
        return cameras != null ? cameras.size() : 0;
    }

    @Override
    public Object getItem(int position)
    {
        return cameras.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return 0;
    }

    public static class ViewHolder
    {
        @Bind(R.id.camera_item_address)
        public TextView addressText;

        @Bind(R.id.camera_item_select)
        public View selectedView;
    }
}
